//
//  SAPWeatherViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/12.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPWeatherViewController.h"

@interface SAPWeatherViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView           *tableView;
@property (nonatomic, retain) SAPWeatherOneModel    *oneModel;
@property (nonatomic, retain) SAPTomorrowTwoModel   *tomorrowTwoModel;
@property (nonatomic, retain) SAPTomorrowOneModel   *tomorrowOneModel;
@property (nonatomic, retain) UIImageView           *backPic;
@property (nonatomic, retain) SAPCityViewController *vc;
@end

@implementation SAPWeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(weatherToCity:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    self.backPic = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backPic.image = [UIImage imageNamed:@"qiche.jpg"];
    
    

    [self.view addSubview:self.backPic];
    [self loadData];
    

}

- (void)loadData {
    if (self.string == nil) {
        _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _HUD.color = [UIColor blackColor];
        _HUD.dimBackground = YES;
        _HUD.labelText = @"正在加载";
    [SAPNetWorkTool getWithUrl:weatherToday parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"weatherinfo"];
     
        self.oneModel = [[SAPWeatherOneModel alloc] initWithDataSource:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    [SAPNetWorkTool getWithUrl:weatherTom parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"data"];
        NSDictionary *dic2 = [dic valueForKey:@"yesterday"];
        self.tomorrowOneModel = [[SAPTomorrowOneModel alloc] initWithDataSource:dic];
        self.tomorrowTwoModel = [[SAPTomorrowTwoModel alloc] initWithDataSource:dic2];
        dispatch_async(dispatch_get_main_queue(), ^{
            _HUD.hidden = YES;
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    [self tableViews];
    }
    
    NSString *string = [NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/%@.html", self.string];
    NSString *string2 = [NSString stringWithFormat:@"http://wthrcdn.etouch.cn/weather_mini?citykey=%@", self.string];
    if (self.string != nil) {
        
    [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"weatherinfo"];
        
        self.oneModel = [[SAPWeatherOneModel alloc] initWithDataSource:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
        
        [SAPNetWorkTool getWithUrl:string2 parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"data"];
        NSDictionary *dic2 = [dic valueForKey:@"yesterday"];
        self.tomorrowOneModel = [[SAPTomorrowOneModel alloc] initWithDataSource:dic];
        self.tomorrowTwoModel = [[SAPTomorrowTwoModel alloc] initWithDataSource:dic2];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
        [self tableViews];
        [self.tableView reloadData];
    }
}
- (void)weatherToCity:(UIBarButtonItem *)city {
    SAPCityViewController *cityVC = [[SAPCityViewController alloc] init];
    [self.navigationController pushViewController:cityVC animated:YES];
}
- (void)leftButtonAction:(UIBarButtonItem *)left {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView registerClass:[SAPWeatherTableViewCell class] forCellReuseIdentifier:@"SAPWeatherTableViewCellTableViewCellIndentifier"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPWeatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPWeatherTableViewCellTableViewCellIndentifier"];

    cell.oneModel = self.oneModel;
    cell.tomorrowOneModel = self.tomorrowOneModel;
    cell.tomorrowTowModel = self.tomorrowTwoModel;
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.768 green:0.735 blue:0.653 alpha:0.0];
    ;
    cell.userInteractionEnabled = NO;
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return SCREEN_SIZE.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)dealloc {
    [_string release];
    [_HUD release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
